import {
    ApiKeyManager
} from "./apiKeyManager.js";
import {
    BalanceManager
} from "./balanceManager.js";
import {
    I18nManager
} from "./i18n.js";
import {
    UiManager
} from "./uiManager.js";
import {
    StorageManager
} from "./storageManager.js";
class PopupManager {
    constructor() {
        this.apiKeyManager = new ApiKeyManager, this.balanceManager = new BalanceManager, this.i18nManager = new I18nManager, this.uiManager = new UiManager, this.storageManager = new StorageManager, this.initializeEventListeners(), this.loadInitialState()
    }
    async loadInitialState() {
        const e = await this.storageManager.getSettings();
        e.apiKey && (this.uiManager.setApiKeyValue(e.apiKey), this.apiKeyManager.lastValidatedValue = e.apiKey, this.handleBalanceRefresh()), this.uiManager.elements.languageSelect.value = e.language, this.uiManager.elements.modelSelect.value = e.model, this.uiManager.elements.selectionEnabled.checked = e.selectionEnabled, this.uiManager.elements.rememberWindowSize.checked = e.rememberWindowSize, this.uiManager.elements.pinWindow.checked = e.pinWindow
    }
    initializeEventListeners() {
        this.uiManager.elements.toggleButton.addEventListener("click", (() => this.uiManager.toggleApiKeyVisibility())), this.uiManager.elements.apiKeyInput.addEventListener("blur", (() => this.handleApiKeyValidation())), this.uiManager.elements.languageSelect.addEventListener("change", (e => this.storageManager.saveLanguage(e.target.value))), this.uiManager.elements.modelSelect.addEventListener("change", (e => this.storageManager.saveModel(e.target.value))), this.uiManager.elements.selectionEnabled.addEventListener("change", (e => this.storageManager.saveSelectionEnabled(e.target.checked))), this.uiManager.elements.rememberWindowSize.addEventListener("change", (e => this.storageManager.saveRememberWindowSize(e.target.checked))), this.uiManager.elements.pinWindow.addEventListener("change", (e => this.storageManager.savePinWindow(e.target.checked))), this.uiManager.elements.balanceToggle.addEventListener("click", (() => this.handleBalanceToggle())), this.uiManager.elements.totalBalance.addEventListener("click", (() => this.handleBalanceToggle())), document.getElementById("shortcutSettings").addEventListener("click", (e => this.handleShortcutSettings(e))), document.getElementById("instructionsLink").addEventListener("click", (e => this.handleInstructionsLink(e)))
    }
    async handleApiKeyValidation() {
        const e = this.uiManager.getApiKeyValue();
        if ("" === e || e === this.apiKeyManager.lastValidatedValue) return;
        this.uiManager.showMessage(this.i18nManager.getTranslation("validating"), !0);
        await this.apiKeyManager.validateApiKey(e) ? (await this.apiKeyManager.saveApiKey(e), this.uiManager.showMessage(this.i18nManager.getTranslation("saveSuccess"), !0), this.handleBalanceRefresh()) : this.uiManager.showMessage(this.i18nManager.getTranslation("apiKeyInvalid"), !1)
    }
    async handleBalanceRefresh() {
        if (this.balanceManager.isLoading()) return;
        const e = await this.apiKeyManager.getApiKey();
        if (e) {
            this.balanceManager.setLoading(!0), this.uiManager.showLoadingState();
            try {
                const a = await this.balanceManager.fetchBalance(e);
                this.uiManager.updateBalanceDisplay(a, this.i18nManager.getTranslation("noBalance"))
            } catch (e) {
                this.uiManager.elements.totalBalance.textContent = this.i18nManager.getTranslation("fetchError")
            } finally {
                this.balanceManager.setLoading(!1)
            }
        } else this.uiManager.elements.totalBalance.textContent = this.i18nManager.getTranslation("noApiKey")
    }
    handleBalanceToggle() {
        this.uiManager.toggleBalance() && "--" === this.uiManager.elements.totalBalance.textContent && this.handleBalanceRefresh()
    }
    handleShortcutSettings(e) {
        e.preventDefault(), chrome.tabs.create({
            url: "chrome://extensions/shortcuts"
        })
    }
    handleInstructionsLink(e) {
        e.preventDefault();
        const a = chrome.runtime.getURL("Instructions/Instructions.html");
        chrome.tabs.create({
            url: a
        })
    }
}
document.addEventListener("DOMContentLoaded", (() => {
    new PopupManager, updateContent()
}));
const translations = {
        zh: {
            headerTitle: "DeepSeek AI",
            apiKeyPlaceholder: "\u5728\u6b64\u8f93\u5165 API Key",
            apiKeyLink: "\u524d\u5f80\u83b7\u53d6 API Key",
            preferredLanguageLabel: "\u9996\u9009\u8bed\u8a00",
            modelLabel: "\u6a21\u578b\u9009\u62e9",
            saveButton: "\u4fdd\u5b58",
            shortcutSettingsText: "\u5feb\u6377\u952e\u8bbe\u7f6e",
            shortcutDescription: "\u8bf7\u524d\u5f80\u8bbe\u7f6e\u5feb\u6377\u952e",
            instructionsText: "\u4f7f\u7528\u8bf4\u660e",
            statusText: "API \u670d\u52a1\u72b6\u6001",
            balanceText: "\u4f59\u989d",
            noBalance: "\u65e0\u53ef\u7528\u4f59\u989d",
            noApiKey: "\u8bf7\u5148\u8bbe\u7f6eAPI Key",
            fetchError: "\u67e5\u8be2\u5931\u8d25",
            apiKeyEmpty: "\u8bf7\u8f93\u5165 API Key",
            apiKeyInvalid: "API Key \u65e0\u6548",
            saveSuccess: "\u8bbe\u7f6e\u5df2\u4fdd\u5b58",
            selectionEnabledLabel: "\u5feb\u6377\u6309\u94ae",
            selectionEnabledTip: "\u9009\u4e2d\u6587\u672c\u540e\u663e\u793a\u5feb\u6377\u6309\u94ae\uff0c\u70b9\u51fb\u53ef\u5feb\u901f\u6253\u5f00\u4f1a\u8bdd\u7a97\u53e3",
            validating: "\u6b63\u5728\u9a8c\u8bc1...",
            rememberWindowSizeLabel: "\u4fdd\u5b58\u7a97\u53e3\u5927\u5c0f",
            rememberWindowSizeTip: "\u8bb0\u4f4f\u60a8\u8c03\u6574\u540e\u7684\u4f1a\u8bdd\u7a97\u53e3\u5927\u5c0f\uff0c\u4e0b\u6b21\u6253\u5f00\u65f6\u5c06\u4fdd\u6301\u76f8\u540c\u5c3a\u5bf8",
            pinWindowLabel: "\u56fa\u5b9a\u7a97\u53e3",
            pinWindowTip: "\u5f00\u542f\u540e\uff0c\u70b9\u51fb\u7a97\u53e3\u5916\u90e8\u4e0d\u4f1a\u81ea\u52a8\u5173\u95ed\u4f1a\u8bdd\u7a97\u53e3"
        },
        en: {
            headerTitle: "DeepSeek AI",
            apiKeyPlaceholder: "Enter API Key here",
            apiKeyLink: "Get API Key",
            preferredLanguageLabel: "Preferred Language",
            modelLabel: "Model Selection",
            saveButton: "Save",
            shortcutSettingsText: "Shortcut Settings",
            shortcutDescription: "Please configure shortcut keys",
            instructionsText: "Instructions",
            statusText: "API Service Status",
            balanceText: "Balance",
            noBalance: "No available balance",
            noApiKey: "Please set API Key first",
            fetchError: "Failed to fetch",
            apiKeyEmpty: "Please enter API Key",
            apiKeyInvalid: "Invalid API Key",
            saveSuccess: "Settings saved",
            selectionEnabledLabel: "Quick Button",
            selectionEnabledTip: "Show a quick button when text is selected to open the chat window",
            validating: "Validating...",
            rememberWindowSizeLabel: "Save Window Size",
            rememberWindowSizeTip: "Remember your preferred chat window size for future sessions",
            pinWindowLabel: "Pin Window",
            pinWindowTip: "When enabled, clicking outside the window won't close it"
        }
    },
    getCurrentLang = () => localStorage.getItem("preferredLang") || "en",
    setCurrentLang = e => localStorage.setItem("preferredLang", e),
    updateContent = () => {
        const e = getCurrentLang(),
            a = translations[e],
            t = {
                "header-title": a.headerTitle,
                apiKey: {
                    placeholder: a.apiKeyPlaceholder
                },
                apiKeyLink: a.apiKeyLink,
                preferredLanguageLabel: a.preferredLanguageLabel,
                modelLabel: a.modelLabel,
                saveButton: a.saveButton,
                shortcutSettingsText: a.shortcutSettingsText,
                shortcutDescription: a.shortcutDescription,
                instructionsText: a.instructionsText,
                statusText: a.statusText,
                balanceText: a.balanceText,
                selectionEnabledLabel: a.selectionEnabledLabel,
                rememberWindowSizeLabel: a.rememberWindowSizeLabel,
                pinWindowLabel: a.pinWindowLabel
            };
        Object.entries(t).forEach((([e, a]) => {
            const t = document.getElementById(e);
            t && ("object" == typeof a ? Object.entries(a).forEach((([e, a]) => {
                t[e] = a
            })) : t.textContent = a)
        }));
        const n = {
            selectionEnabledLabel: a.selectionEnabledTip,
            rememberWindowSizeLabel: a.rememberWindowSizeTip,
            pinWindowLabel: a.pinWindowTip
        };
        Object.entries(n).forEach((([e, a]) => {
            const t = document.getElementById(e);
            t && t.setAttribute("data-tooltip", a)
        }));
        const i = document.getElementById("totalBalance");
        if (i) {
            const e = i.textContent,
                t = {
                    \u65e0\u53ef\u7528\u4f59\u989d: a.noBalance,
                    "\u8bf7\u5148\u8bbe\u7f6eAPI Key": a.noApiKey,
                    \u67e5\u8be2\u5931\u8d25: a.fetchError
                };
            t[e] && (i.textContent = t[e])
        }
        document.getElementById("language").value = e
    };
document.getElementById("language-toggle").addEventListener("click", (() => {
    const e = getCurrentLang();
    setCurrentLang("zh" === e ? "en" : "zh"), updateContent()
})), document.getElementById("language").addEventListener("change", (e => {}));